﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.SysManage;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LJX.SmartPark.Service.Area.MonthcardServeice;
using static LJX.SmartPark.Service.SafeManage.TrainServeice;

namespace LJX.SmartPark.Service.SysManage
{
    public class UserManageServeice:IUserManageServeice
    {
        private readonly IRepository<UserManage> _userManageService;

        public UserManageServeice(IRepository<UserManage> userManageService)
        {
            _userManageService = userManageService;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeleteUserManageAsync(UserManage input)
        {
            var result = await _userManageService.DeleteByIdAsync(input.MapTo<UserManage>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return _userManageService.GetCount();
        }

        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="fy1"></param>
        /// <returns></returns>
        public async Task<Response<List<UserManage>>> GetUserManageAsync(FyUserManage fyUserManage)
        {
            var students = await _userManageService.GetAllListAsync();
            var result = students.MapToList<UserManage>();

            if (!string.IsNullOrEmpty(fyUserManage.personName))
            {
                result = result.Where(a => a.PersonName.Contains(fyUserManage.personName)).ToList();
            }

            var totalCount = result.Count();

            result = result.Skip(((fyUserManage.pageIndex - 1) * fyUserManage.pageSize)).Take(fyUserManage.pageSize).ToList();

            return Response<List<UserManage>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<UserManage> GetUserManageById(UserManage input)
        {
            var userManage = _userManageService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = userManage.MapTo<UserManage>();
            return Response<UserManage>.Ok(result);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUpdUserManageAsync(UserManage input)
        {
            var result = _userManageService.UpdateAsync(input.MapTo<UserManage>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        public class FyUserManage
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string? personName { get; set; }
        }
    }
}
